#ifndef ETHERNET_H
#define ETHERNET_H

#include <QObject>
#include <QTcpSocket>
#include <QUdpSocket>
#include <QProcess>
#include <QTimer>
#include <QByteArray>
#include <QTime>

class Ethernet : public QObject
{
    Q_OBJECT
public:
    explicit Ethernet(QObject *parent = nullptr);

    ~Ethernet() {
        delete tcpSocket;
        delete udpSocket;
        delete pingProcess;
    }

    Q_INVOKABLE void connectToHost(const QString &protocol, const QString &host, quint16 port);
    Q_INVOKABLE void sendMessage(const QString &message);
    Q_INVOKABLE void disconnectFromHost();
    Q_INVOKABLE void pingHost(const QString &host);
    Q_INVOKABLE void startSpeedTest(const QString &protocol, const QString &host, quint16 port, int duration);
    Q_INVOKABLE void stopSpeedTest();

signals:
    void messageReceived(const QString &message);
    void connectionEstablished();
    void connectionError(const QString &error);
    void connectionRefused();
    void pingReply(const QString &reply);
    void speedTestResult(double sentMbps, double receivedMbps);

public slots:
    void onReadyRead();
    void onConnected();
    void onDisconnected();
    void onError(QAbstractSocket::SocketError socketError);
    void onPingFinished();
    void onPingOutput();
    void onPingError();
    void sendData();
    void calculateSpeed();

private:
    QTcpSocket *tcpSocket;
    QUdpSocket *udpSocket;
    QProcess *pingProcess;
    QString currentProtocol;
    QByteArray testData;
    QTime startTime, endTime;
    qint64 dataSent = 0, dataReceived = 0;
    bool isTesting = false;
    QString testHost; // 新增成员变量
    quint16 testPort; // 新增成员变量
};

#endif // ETHERNET_H
